package cn.com.ddapp.ddm.ui.shopcar;

import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.ActivityManager;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.shopcar.GoodsCar;
import cn.com.ddapp.ddm.bean.shopcar.Shopcar;
import cn.com.ddapp.ddm.bean.shopcar.ShopcarEntity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.dialog.CommonDialog;
import cn.com.ddapp.ddm.ui.dialog.DialogHelper;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TLog;
import cn.com.ddapp.ddm.widget.AddAndSubView;

/**
 * Created by caopenghui on 2016-08-05.
 */
public class ShopcarEditActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.action)
    TextView action;

    @BindView(R.id.listView)
    ExpandableListView listView;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    @BindView(R.id.balanceCheckBox)
    CheckBox mBalanceCheckBox;

    @BindView(R.id.delete)
    TextView mDelete;

    @BindView(R.id.follow)
    TextView follow;


    private List<Shopcar> shopcars = new ArrayList<Shopcar>();// 保存一级目录的商家
    private HashMap<Object, List<GoodsCar>> goodsCarMap = new HashMap<Object, List<GoodsCar>>();// 保存二级目录的购物车

    //保存子childbox的选中状态
    private HashMap<Integer, HashMap<Integer, Boolean>> isChildSelected = new HashMap<Integer, HashMap<Integer, Boolean>>();
    //保存groupbox的选中状态
    private HashMap<Integer, Boolean> isGroupSelected = new HashMap<Integer, Boolean>();
    private List<GoodsCar> mgoodsList = new ArrayList<>();
    private ExpandableListView.OnGroupClickListener onGroupClickListener;
    private boolean isAuto = false;//全选checkbox是手动点击还是自动变化 默认手动
    private int lastGroupSelectedSize = 0;
    private int lastChildSelectedSize = 0;
    private HashSet<Integer> selectedCarsId = new HashSet<>();//选中的car id集合

    @Override
    public void initView() {
        title.setText(R.string.shop_car);
        action.setText(R.string.finish);
        action.setVisibility(View.VISIBLE);
        action.setOnClickListener(this);
        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearData();
                requestData();
            }
        });
        mDelete.setText(BaseApplication.getIntstance().getResources().getString(R.string.delete) + "(0)");
        mDelete.setOnClickListener(this);
        follow.setOnClickListener(this);
        mBalanceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                Integer totalCount = 0;
                if (isCheck) {
                    for (int i = 0; i < shopcars.size(); i++) {
                        isGroupSelected.put(i, isCheck);
                    }
                    Set<Object> cityMapSets = goodsCarMap.keySet();
                    int index = 0;
                    for (int i = 0; i < cityMapSets.size(); i++) {
                        HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
                        List<GoodsCar> lists = goodsCarMap.get(i);
                        for (int k = 0; k < lists.size(); k++) {
                            hashMap.put(Integer.valueOf(k), isCheck);
                        }
                        isChildSelected.put(index, hashMap);
                        index++;
                    }
                    totalCount=mgoodsList.size();
                    mDelete.setText(BaseApplication.getIntstance().getResources().getString(R.string.delete) + "(" + totalCount + ")");
                    lastGroupSelectedSize = isGroupSelected.size();
                } else {
                    if (!isAuto) {
                        for (int i = 0; i < shopcars.size(); i++) {
                            isGroupSelected.put(i, isCheck);
                        }
                        Set<Object> cityMapSets = goodsCarMap.keySet();
                        int index = 0;
                        for (int i = 0; i < cityMapSets.size(); i++) {
                            HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
                            List<GoodsCar> lists = goodsCarMap.get(i);
                            for (int k = 0; k < lists.size(); k++) {
                                hashMap.put(Integer.valueOf(k), isCheck);
                            }
                            isChildSelected.put(index, hashMap);
                            index++;
                        }
                        mDelete.setText(BaseApplication.getIntstance().getResources().getString(R.string.delete) + "(" + 0 + ")");
                    }
                    isAuto = false;
                }

                isAuto = false;//重置为手动点击
                //刷新视图
                ((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
            }
        });
    }

    //改变child checkbox的状态
    private void autoChangeChildCheckeState(boolean isChecked, int groupPosition) {

        HashMap<Integer, Boolean> map = isChildSelected.get(groupPosition);
        for (int i = 0; i < map.size(); i++) {
            map.put(Integer.valueOf(i), isChecked);
        }
        isChildSelected.put(Integer.valueOf(groupPosition), map);
        calcItemsAndSize();

        boolean isAllSelect = true;//默认全部选中
        int count = 0;
        for (int i = 0; i < shopcars.size(); i++) {
            if (isGroupSelected.get(i)) {
                count++;
            }
        }
        for (int i = 0; i < shopcars.size(); i++) {
            if (!isGroupSelected.get(i)) {
                isAllSelect = false;
                break;
            }
        }
        if (lastGroupSelectedSize - count == 1 && lastGroupSelectedSize == isGroupSelected.size()) {
            isAuto = true;//从全选到去掉一个
        }
        lastGroupSelectedSize = count;

        mBalanceCheckBox.setChecked(isAllSelect);

    }

    //获取某组下的子child的选中个数
    private int getPositionCheckedNum(HashMap<Integer, HashMap<Integer, Boolean>> map, Integer position) {
        int count = 0;
        HashMap<Integer, Boolean> map1 = map.get(position);
        for (int i = 0; i < map1.size(); i++) {
            if (map1.get(i)) {
                count++;
            }
        }
        return count;
    }

    //group checkbox的状态
    private void autoChangeGroupCheckeState(HashMap<Integer, HashMap<Integer, Boolean>> isChildSelected, int groupPosition, boolean isChecked) {
//        Boolean currentSelected = isGroupSelected.get(groupPosition);
        int childSelectedCount = getPositionCheckedNum(isChildSelected, groupPosition);
        if (isChecked && (childSelectedCount == isChildSelected.get(groupPosition).size())) {
            isGroupSelected.put(Integer.valueOf(groupPosition), true);
        } else if ((!isChecked) && (childSelectedCount == isChildSelected.get(groupPosition).size() - 1)) {
            isGroupSelected.put(Integer.valueOf(groupPosition), false);
        }
        calcItemsAndSize();

        boolean isAllSelect = true;//默认全部选中
        int count = 0;
        for (int i = 0; i < shopcars.size(); i++) {
            if (isGroupSelected.get(i)) {
                count++;
            }
        }
        for (int i = 0; i < shopcars.size(); i++) {
            if (!isGroupSelected.get(i)) {
                isAllSelect = false;
                break;
            }
        }
        if (lastGroupSelectedSize - count == 1 && lastGroupSelectedSize == isGroupSelected.size()) {
            isAuto = true;//从全选到去掉一个
        }
        lastGroupSelectedSize = count;

        mBalanceCheckBox.setChecked(isAllSelect);

    }

    //刷新视图并计算出总的总的购物车个数及金额
    private void calcItemsAndSize() {
        //刷新视图
        ((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
//        adapter.
        //计算总的items size
        int count = 0;//总选中items数量
        double balance = 0.00;
        for (int i = 0; i < isChildSelected.size(); i++) {
            HashMap<Integer, Boolean> map = isChildSelected.get(i);
            for (int k = 0; k < map.size(); k++) {
                if (map.get(k)) {
                    count++;
                    balance = balance + (goodsCarMap.get(i).get(k).getPrice()) * (goodsCarMap.get(i).get(k).getNum());
                }
            }
        }

        mDelete.setText(BaseApplication.getIntstance().getResources().getString(R.string.delete) + "(" + count + ")");
    }

    final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            TextView group_text;
            CheckBox group_box;
//			if (convertView == null) {
            convertView = RelativeLayout.inflate(ShopcarEditActivity.this, R.layout.shopcar_item_shop, null);
//			}

            group_text = (TextView) convertView.findViewById(R.id.sName);
            group_box = (CheckBox) convertView.findViewById(R.id.sCheckBox);

            group_text.setText(shopcars.get(groupPosition).getShopName());
            group_box.setChecked(isGroupSelected.get(Integer.valueOf(groupPosition)));

            group_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    isGroupSelected.put(Integer.valueOf(groupPosition), isChecked);
                    autoChangeChildCheckeState(isChecked, groupPosition);
                }
            });
            return convertView;
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            return shopcars.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return shopcars.get(groupPosition).getShopName();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return goodsCarMap.get(groupPosition).size();
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            GoodsCar goodsCar = goodsCarMap.get(groupPosition).get(childPosition);
//			if (convertView == null) {
            convertView = RelativeLayout.inflate(ShopcarEditActivity.this, R.layout.shopcar_item_car_edit, null);
//			}
            ViewHolder holder = new ViewHolder(convertView, goodsCar,groupPosition,childPosition);
            holder.gName.setText(goodsCarMap.get(groupPosition).get(childPosition).getGoodsName());
//            holder.citySize.setText(StringUtils.getDouble((cityMap.get(groupPosition).get(childPosition).getSize())/(1024 * 1024f), 2) + "MB");
            holder.gCheckBox.setChecked(isChildSelected.get(Integer.valueOf(groupPosition)).get(Integer.valueOf(childPosition)));
            holder.gAttr.setText(goodsCarMap.get(groupPosition).get(childPosition).getAttrName());
            holder.gPrice.setText(new DecimalFormat("######0.00").format(goodsCarMap.get(groupPosition).get(childPosition).getPrice()));

            String []nUrl=goodsCarMap.get(groupPosition).get(childPosition).getGoodsImg().split("\\.");
            StringBuffer buffer=new StringBuffer();
            buffer.append(nUrl[0]).append("_360.").append(nUrl[1]);
//            if(nUrl[1].equals("jpg")){
//                buffer.append(nUrl[0]).append("_360.jpg");
//            }else if(nUrl[1].equals("png")){
//                buffer.append(nUrl[0]).append("_360.png");
//            }else if(nUrl[1].equals("jpeg")){
//                buffer.append(nUrl[0]).append("_360.jpeg");
//            }
//            buffer.append(nUrl[0]).append("_360.jpg");
            Glide.with(ShopcarEditActivity.this).load(OKUtilAPI.imgBaseUrl + buffer).into(holder.gLogo);

            holder.gCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    HashMap<Integer, Boolean> map = isChildSelected.get(Integer.valueOf(groupPosition));
                    map.put(Integer.valueOf(childPosition), isChecked);
                    isChildSelected.put(Integer.valueOf(groupPosition), map);
                    //改变group的checkbox状态
                    autoChangeGroupCheckeState(isChildSelected, groupPosition, isChecked);
                }

            });
            return convertView;
        }

        class ViewHolder {
            TextView gPrice;
            TextView gName;
            TextView gAttr;
            CheckBox gCheckBox;
            ImageView gLogo;
            AddAndSubView addAndSubView;

            public ViewHolder(View view, GoodsCar goodsCar, final int groupPosition, final int childPosition) {
                gCheckBox = (CheckBox) view.findViewById(R.id.gCheckBox);
                gLogo = (ImageView) view.findViewById(R.id.gLogo);
                gName = (TextView) view.findViewById(R.id.gName);
                gAttr = (TextView) view.findViewById(R.id.gAttr);
                gPrice = (TextView) view.findViewById(R.id.gPrice);
                addAndSubView = (AddAndSubView) view.findViewById(R.id.addAndSubView);

                addAndSubView.setCount(goodsCar.getNum());
                addAndSubView.setAddAndSubClicker(new AddAndSubView.OnAddAndSubClicker() {
                    @Override
                    public void onAdd() {
                        editCarNumRequest(addAndSubView.getCount(),groupPosition,childPosition);
                    }

                    @Override
                    public void onSub() {
                        editCarNumRequest(addAndSubView.getCount(),groupPosition,childPosition);
                    }
                });
            }

        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return goodsCarMap.get(groupPosition).get(childPosition).getShopName();
        }
    };


    private void requestData() {
        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        String userId =  SPUtils.getStringData(this, SPUtils.USERID);

        final OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                TLog.log(getFragmentTag(), response);
                ShopcarEntity entity = gson.fromJson(response, ShopcarEntity.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                    mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else {
//                    List<Shopcar> lists = entity.getList();

                    shopcars = entity.getList();
                    for (int i = 0; i < shopcars.size(); i++) {
                        Shopcar shopCar = shopcars.get(i);
                        List<GoodsCar> city = new ArrayList<GoodsCar>();
                        city.addAll(shopCar.getGoodsList());
                        goodsCarMap.put(i, city);
                        mgoodsList.addAll(shopCar.getGoodsList());
                    }
                    for (int i = 0; i < shopcars.size(); i++) {
                        isGroupSelected.put(i, false);
                    }
                    Set<Object> cityMapSets = goodsCarMap.keySet();
                    int index = 0;
                    for (int i = 0; i < cityMapSets.size(); i++) {
                        HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
                        List<GoodsCar> lists = goodsCarMap.get(i);
                        for (int k = 0; k < lists.size(); k++) {
                            hashMap.put(Integer.valueOf(k), false);
                        }
                        isChildSelected.put(index, hashMap);
                        index++;
                    }

                    listView.setAdapter(adapter);
                    onGroupClickListener = new ExpandableListView.OnGroupClickListener() {

                        @Override
                        public boolean onGroupClick(ExpandableListView parent, View v,
                                                    int groupPosition, long id) {
                            // TODO Auto-generated method stub

//                            if(parent.isGroupExpanded(groupPosition)){
//                                parent.collapseGroup(groupPosition);
//                            }else{
////第二个参数false表示展开时是否触发默认滚动动画
//                                parent.expandGroup(groupPosition,false);
//                            }
                            Log.i(getFragmentTag(), "setOnGroupClickListener()");
                            return true;
                        }

                    };
                    listView.setOnGroupClickListener(onGroupClickListener);

                    //设置 属性 GroupIndicator 去掉默认向下的箭头
                    listView.setGroupIndicator(null);

                    for (int i = 0; i < adapter.getGroupCount(); i++) {
                        listView.expandGroup(i, false);
                    }
                }
            }
        };
        OKUtilAPI.post(OKUtilAPI.shopList, "userId=" + userId+"&addrId="+AppConfig.CITY_ID, okUtilCallback);
    }

    @Override
    public void initData() {
        requestData();
    }

    protected String getFragmentTag() {
        return ShopcarEditActivity.class.getSimpleName();
    }

    @Override
    public void onClick(View view) {
        if (mDelete.getId() == view.getId()) {
            for (int i = 0; i < shopcars.size(); i++) {
                HashMap<Integer, Boolean> map = isChildSelected.get(i);
                for (int k = 0; k < map.size(); k++) {
                    if (map.get(k)) {
                        selectedCarsId.add(goodsCarMap.get(i).get(k).getId());
                    }
                }
            }
            if (selectedCarsId.size() <= 0) {
                BaseApplication.getIntstance().showToast(BaseApplication.getIntstance().getString(R.string.go_balance_error_zero), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
            } else {
                delCarsRequest();
            }
        }else if(view.getId()==action.getId()){
            ActivityManager.getInstance().finishActivity();
        }else if(follow.getId()==view.getId()){
            for (int i = 0; i < shopcars.size(); i++) {
                HashMap<Integer, Boolean> map = isChildSelected.get(i);
                for (int k = 0; k < map.size(); k++) {
                    if (map.get(k)) {
                        selectedCarsId.add(goodsCarMap.get(i).get(k).getId());
                    }
                }
            }
            if (selectedCarsId.size() <= 0) {
                BaseApplication.showToast(R.string.select_follow_goods);
            } else {
                followGoodsRequest();
            }
        }
    }

    /**
     * 移入关注
     */
    private void followGoodsRequest() {

        StringBuilder ids = new StringBuilder();
        for (Integer id : selectedCarsId) {
            ids.append(id + ",");
        }
        if (ids.toString().length() <= 0) {
            BaseApplication.getIntstance().showToast(R.string.select_follow_goods);
            return;
        }

        showWaitDialog("已收藏");
        String userId =  SPUtils.getStringData(ShopcarEditActivity.this, SPUtils.USERID);
        final OkUtilCallback okUtilCallback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                hideWaitDialog();
                TLog.log(getFragmentTag(), response);
                Entity entity = gson.fromJson(response, Entity.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                    mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.showToast(R.string.goods_follow_success);
                    selectedCarsId.clear();
                }
            }
        };
        OKUtilAPI.post(OKUtilAPI.goodsFollow, "userId=" + userId + "&ids=" + ids.substring(0, ids.length() - 1), okUtilCallback);
    }

    /**
     * 编辑购物车数量
     * @param carNum
     * @param groupPosition
     * @param childPosition
     */
    private void editCarNumRequest(final Integer carNum, final int groupPosition, int childPosition) {
        showWaitDialog();
        String userId =  SPUtils.getStringData(this, SPUtils.USERID);
        final GoodsCar car = goodsCarMap.get(groupPosition).get(childPosition);
        final OkUtilCallback okUtilCallback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                hideWaitDialog();
                TLog.log(getFragmentTag(), response);
                Entity entity = gson.fromJson(response, Entity.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                    mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                    car.setNum(carNum);
                    ((BaseExpandableListAdapter)adapter).notifyDataSetChanged();
                    AppConfig.isRefreshShocpar = true;
                }
            }
        };

        OKUtilAPI.post(OKUtilAPI.editCars, "userId=" + userId + "&num=" + carNum+"&goodsId=" + car.getGoodsId()+"&shopId=" + car.getShopId()+"&attrs="+car.getAttrs() + "&type=2", okUtilCallback);
    }

    /**
     * 删除购物车
     */
    private void delCarsRequest() {

        int count = 0;//总选中items数量
        for (int i = 0; i < isChildSelected.size(); i++) {
            HashMap<Integer, Boolean> map = isChildSelected.get(i);
            for (int k = 0; k < map.size(); k++) {
                if (map.get(k)) {
                    count++;
                }
            }
        }
        final CommonDialog dialog = DialogHelper
                .getPinterestDialogCancelable(ShopcarEditActivity.this);
        dialog.setTitle(String.format("确定删除选中的商品?",count));
        dialog.setNegativeButton(BaseApplication.getIntstance().getResources().getString(R.string.cancel), null);
        dialog.setPositiveButton(BaseApplication.getIntstance().getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
                StringBuilder ids = new StringBuilder();
                for (Integer id : selectedCarsId) {
                    ids.append(id + ",");
                }
                if (ids.toString().length() <= 0) {
                    BaseApplication.getIntstance().showToast(BaseApplication.getIntstance().getString(R.string.go_balance_error_zero), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    return;
                }
                showWaitDialog(R.string.shop_car_delete);

                String userId =  SPUtils.getStringData(ShopcarEditActivity.this, SPUtils.USERID);
                final OkUtilCallback okUtilCallback = new OkUtilCallback() {
                    @Override
                    public void onMyResponse(String response, int id) {
                        hideWaitDialog();
                        TLog.log(getFragmentTag(), response);
                        Entity entity = gson.fromJson(response, Entity.class);
                        if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                            mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                        } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                        } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {

//                            clearData();
//
//                            requestData();
                            ActivityManager.getInstance().finishActivity();
                            AppConfig.isRefreshShocpar = true;
                        }
                    }
                };
                OKUtilAPI.post(OKUtilAPI.delCars, "userId=" + userId + "&ids=" + ids.substring(0, ids.length() - 1), okUtilCallback);
            }
        });
        dialog.show();

    }

    private void clearData() {
        shopcars.clear();
        mgoodsList.clear();
        Set<Object> cityMapSets = goodsCarMap.keySet();
        for (int i = 0; i < cityMapSets.size(); i++) {
            HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
            List<GoodsCar> lists = goodsCarMap.get(i);
            lists.clear();
        }

        selectedCarsId.clear();
        for (int i = 0; i < shopcars.size(); i++) {
            HashMap<Integer, Boolean> map = isChildSelected.get(i);
            for (int k = 0; k < map.size(); k++) {
                map.put(k, false);
            }
        }
        for (int i = 0; i < shopcars.size(); i++) {
            isGroupSelected.put(i, false);
        }
        isAuto = false;
        lastGroupSelectedSize = 0;
        lastChildSelectedSize = 0;
    }


    @Override
    public int getContentViewId() {
        return R.layout.shopcar_edit_activity;
    }


}
